package org.MyToolsProject;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class TxtUtil {
    public static void main(String[] args) {
        t1();
    }


    public static void t3(){
        String str = "6656:M 25 Oct 2024 08:53:00.308 - Accepted 10.145.0.182:40764";
        String[] infos = str.split(" ");

        Node node = new Node();
        String[] pids = infos[0].split(":");
        node.setPid(pids[0]);
        node.setRole(pids[1]);
        String time = infos[1] + " " + infos[2]+ " "  + infos[3]+ " "  + infos[4];
        node.setLog_time( time );
        node.setEvent_type( infos[6] );

        String[] clientInfo = infos[7].split(":");
        node.setClient_ip( clientInfo[0] );
        node.setClient_port( clientInfo[1] );
        System.out.println(node);
    }

    public static class Node {
        String pid;
        String role;
        String log_time;
        String event_type;
        String client_ip;
        String client_port;

        public String getPid() {
            return pid;
        }

        public void setPid(String pid) {
            this.pid = pid;
        }

        public String getRole() {
            return role;
        }

        public void setRole(String role) {
            this.role = role;
        }

        public String getLog_time() {
            return log_time;
        }

        public void setLog_time(String log_time) {
            this.log_time = log_time;
        }

        public String getEvent_type() {
            return event_type;
        }

        public void setEvent_type(String event_type) {
            this.event_type = event_type;
        }

        public String getClient_ip() {
            return client_ip;
        }

        public void setClient_ip(String client_ip) {
            this.client_ip = client_ip;
        }

        public String getClient_port() {
            return client_port;
        }

        public void setClient_port(String client_port) {
            this.client_port = client_port;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "pid='" + pid + '\'' +
                    ", role='" + role + '\'' +
                    ", log_time='" + log_time + '\'' +
                    ", event_type='" + event_type + '\'' +
                    ", client_ip='" + client_ip + '\'' +
                    ", client_port='" + client_port + '\'' +
                    '}';
        }
    }


    public static void t2(){
        String dateString = "25 Oct 2024 08:53:00.308";
        SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss.SSS", Locale.ENGLISH);
        try {
            Date date = sdf.parse(dateString);
            long timestamp = date.getTime();
            System.out.println("Timestamp: " + timestamp);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public static void t1(){
        long lineNum = 0;
        Map<String,Long> acceptedMap = new HashMap<>();
        try {
            File file = new File("F:\\工作\\公司\\现场处理\\广西高速ETL3\\ETL_log\\rds\\15600.log");
            Scanner scanner = new Scanner(file);
            String line;
            while (scanner.hasNextLine()) {
                line = scanner.nextLine();
                if(line != null && line.contains("Accepted")){
                    lineNum += 1;
                    line = line.substring( line.indexOf("Accepted ") + "Accepted ".length()  , line.lastIndexOf(":"));
                    Long acceptedNum = acceptedMap.get(line);
                    if(acceptedNum == null){
                        acceptedMap.put(line,1L);
                    }else {
                        acceptedMap.put(line,acceptedNum + 1);
                    }
                }
                if(lineNum % 2000 == 0){
                    System.out.println("第"+ lineNum +"条....");
                }
            }
            scanner.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Accepted size:" + lineNum);
        System.out.println("acceptedMap size:" + acceptedMap.size() );
    }

}
